package com.zxy.ziems.server.statistics.task;

import com.zxy.ziems.server.statistics.service.EnergyFlowService;
import com.zxy.ziems.server.sync.enums.SyncMetaType;
import com.zxy.ziems.server.sync.service.SyncMetaService;
import com.zxy.ziems.server.utils.AssertUtil;
import com.zxy.ziems.server.utils.RedissonLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.zxy.ziems.server.constant.RedisKeyLockConstant.ENERGY_FLOW_DATA_MONTH_KEY;
import static com.zxy.ziems.server.constant.RedisKeyLockConstant.ENERGY_FLOW_DATA_DAY_KEY;

/**
 * 能流图任务
 */
@Slf4j
@Component
public class EnergyFlowTask {

    @Autowired
    private RedissonLockUtil redissonLockUtil;

    @Autowired
    private SyncMetaService syncMetaService;

    @Autowired
    private EnergyFlowService energyFlowService;

    /**
     * 天任务
     * 水、电、气
     */
    @Scheduled(cron = "0 5 1 * * ?")
    public void syncEnergyFlowDataDay() {
        log.info("能流图天数据统计");
        String key = ENERGY_FLOW_DATA_DAY_KEY;

        RLock lock = redissonLockUtil.lock(key);
        lock.lock();

        try {
            // 查看当天任务是否执行
            LocalDateTime syncTime = syncMetaService.getSyncTime(SyncMetaType.ENERGY_FLOW_DATA_DAY_KEY.getCode());
            AssertUtil.nonNull(syncTime, "未配置执行任务");
            LocalDate date = syncTime.toLocalDate().plusDays(1);
            LocalDate yesterday = LocalDate.now().plusDays(-1);
            while (true) {
                if (!(date.compareTo(yesterday) < 1)) {
                    break;
                }
                date = energyFlowService.syncDataDay(date);
            }
            syncMetaService.saveSyncTime(date.atStartOfDay(), SyncMetaType.ENERGY_FLOW_DATA_DAY_KEY.getCode());
        } catch (Exception e) {
            log.error("syncEnergyFlowDataDay err=", e);
            throw new RuntimeException(e);
        } finally {
            log.info("unlock");
            redissonLockUtil.unlock(key);
        }
    }

    /**
     * 月任务
     * 水、电、气
     */
    @Scheduled(cron = "0 5 0 1 * ?")
    public void syncEnergyFlowDataMonth () {
        log.info("月数据统计");
        String key = ENERGY_FLOW_DATA_MONTH_KEY;

        RLock lock = redissonLockUtil.lock(key);
        lock.lock();

        try {
            // 查看当天任务是否执行
            LocalDateTime syncTime = syncMetaService.getSyncTime(SyncMetaType.ENERGY_FLOW_DATA_MONTH_KEY.getCode());
            AssertUtil.nonNull(syncTime, "未配置执行任务");
            LocalDate date = syncTime.toLocalDate().plusMonths(1);
            LocalDate yesterday = LocalDate.now().plusMonths(-1);
            while (true) {
                if (!(date.compareTo(yesterday) < 1)) {
                    break;
                }
                date = energyFlowService.syncDataMonth(date);
            }
            syncMetaService.saveSyncTime(date.atStartOfDay(), SyncMetaType.ENERGY_FLOW_DATA_MONTH_KEY.getCode());
        } catch (Exception e) {
            log.error("syncDeviceMeasureDataDay err=", e);
            throw new RuntimeException(e);
        } finally {
            log.info("unlock");
            redissonLockUtil.unlock(key);
        }
    }

}
